//
//  PlSTableView.m
//  PlugSuit
//
//  Created by ∞ on 22/12/07.
//  Copyright 2007 Emanuele Vulcano (infinite-labs.net). All rights reserved.
//

#import "PlSTableView.h"
#import <QuartzCore/QuartzCore.h>

@implementation PlSTableView

- (id) initWithFrame:(NSRect) frame {
	if (self = [super initWithFrame:frame])
		[self awakeFromInit];
	
	return self;
}

- (id) initWithCoder:(NSCoder*) coder {
	if (self = [super initWithCoder:coder])
		[self awakeFromInit];
	
	return self;
}

- (void) awakeFromInit {
	L0LogS(@"reporting for duty.");
	[self filters];
	//[[self window] setAcceptsMouseMovedEvents:YES];
	
	[self registerForDraggedTypes:
	 [NSArray arrayWithObject:NSFilenamesPboardType]];
}

- (NSArray*) filters {
	static NSArray* arr;
	if (!arr) {
//		CIFilter* bloom = [CIFilter filterWithName:@"CIBloom" keysAndValues:@"inputRadius", [NSNumber numberWithInt:5], @"inputIntensity", [NSNumber numberWithFloat:0.5], nil];
		
		NSColor* col = [[NSColor alternateSelectedControlColor] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
		CIColor* cicol = [CIColor colorWithRed:[col redComponent] green:[col greenComponent] blue:[col blueComponent] alpha:[col alphaComponent]];
		
		CIFilter* mono = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:@"inputColor", cicol, @"inputIntensity", [NSNumber numberWithFloat:0.9], nil];
		
		CIFilter* adj = [CIFilter filterWithName:@"CIColorControls" keysAndValues:
						 @"inputSaturation", [NSNumber numberWithFloat:2.0],
						 @"inputBrightness", [NSNumber numberWithFloat:0],
						 @"inputContrast", [NSNumber numberWithFloat:1.0],
						 nil];
		
		arr = [[NSArray alloc] initWithObjects:/*bloom,*/ mono, adj, nil];
	}
	
	return arr;
}

- (NSDragOperation) draggingEntered:(id <NSDraggingInfo>) sender {
	if ([[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType]) {
		_dragging = YES;
		
		[self display];

		[self setWantsLayer:YES];
		[[self superview] setWantsLayer:YES];
		CALayer* layer = [[self superview] layer];
		[layer setFilters:[self filters]];
		
		L0Log(@"filters = %@", layer.filters);
		
		return NSDragOperationCopy;
	} else
		return NSDragOperationNone;
}

- (NSDragOperation) draggingUpdated:(id <NSDraggingInfo>)sender {
	return [[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType]? NSDragOperationCopy : NSDragOperationNone;
}

- (void) draggingExited:(id <NSDraggingInfo>) sender {
	_dragging = NO;
	[self performSelector:@selector(_clearCoreAnimation) withObject:nil afterDelay:10.0];

	[self display];
	
	CALayer* layer = [[self superview] layer];
	layer.filters = nil;
}

- (BOOL) prepareForDragOperation:(id <NSDraggingInfo>) sender {
	return [[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType];
}

- (BOOL) performDragOperation:(id <NSDraggingInfo>) sender {
	self.draggingCanceled = YES;
	if (![[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType])
		goto returnVal;
	
	[self setWantsLayer:NO];
	[[self superview] setWantsLayer:NO];
	
	if (self.dragActionSelector) {
		NSArray* arr = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
		for (NSString* file in arr)
			[_owner performSelector:self.dragActionSelector withObject:file];
	}
	
returnVal:
	_dragging = NO;
	[self setNeedsDisplay];
	return !self.draggingCanceled;
}

- (void) _clearCoreAnimation {
	if (!_dragging) {
		[self setWantsLayer:NO];
		[[self superview] setWantsLayer:NO];
	}
}

- (void) drawRect:(NSRect) rect {
	[super drawRect:rect];
	
	if (_dragging) {
		[[NSColor selectedControlColor] set];
		NSFrameRectWithWidth([self frame], 4.0);
	}
}

@synthesize dragActionSelector = _dragActionSelector, draggingCanceled = _draggingCanceled;

@end

